
pub mod log_level;
pub mod log_level_visit;

use log_level::LogLevel;
use serde::{Serialize, Deserialize};
use crate::util::clap_helper::clap_arg::CMD_LOG_LEVEL;

pub static RUN_SETUP_CONFIG: once_cell::sync::Lazy<RunSetupConfig> = once_cell::sync::Lazy::new(|| {
    let run_setup_config = RunSetupConfig::init();
    return run_setup_config;
});

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunSetupConfig {
  pub app_list_file: String,
  pub log_root: String,
  pub log_cache: String,
  pub log_level: Option<LogLevel>,
}

impl RunSetupConfig {
  pub fn init() -> RunSetupConfig {
    let file_path = crate::util::path_helper::get_run_setup_path();
    println!("run setup file path: {}", file_path);
    crate::util::yaml_helper::read_yaml_file::<RunSetupConfig>(file_path.as_str())
  }

  pub fn get_log_level(&self) -> LogLevel {
    let cmd_log_level = CMD_LOG_LEVEL.get().expect("cannot get CMD_LOG_LEVEL");
    if cmd_log_level.is_some() {
      return cmd_log_level.unwrap();
    }
    if self.log_level.is_some() {
      return self.log_level.unwrap();
    }
    return LogLevel::Info;
  }

  pub fn get_is_print_log(&self, log_level: &LogLevel) -> bool {
    self.get_log_level().to_u8() <= log_level.to_u8()
  }
}
